/*
** check_slot.c for  in /home/renard/Dropbox/epitech/dev/projets/systemunix/zappy/zappy-2015-2014s-renard_g/serv-src
**
** Made by guillaume renard
** Login   <renard_g@epitech.net>
**
** Started on  Wed Jul 04 18:02:51 2012 guillaume renard
** Last update Thu Jul 19 18:11:32 2012 pierrick druart
*/

#include	<stdlib.h>
#include	<string.h>

#include	"server.h"

int		check_winner_egg(t_list *teams, long unsigned int elapsed_time)
{
  t_egg		*egg;
  t_team	*team;
  t_item	*cur;
  t_item	*cur_egg;

  cur = teams->head;
  while (cur != NULL)
  {
    team = (struct s_team *)cur->content;
    cur_egg = team->egg->head;
    while (cur_egg != NULL)
    {
      egg = (struct s_egg *)cur_egg->content;
      if (egg->action < elapsed_time)
	cur_egg = team->egg->erase(team->egg, cur_egg, &free);
      else
	egg->action -= elapsed_time;
      if (cur_egg != NULL)
	cur_egg = cur_egg->next;
    }
    if (team->nb_maxlvl >= 6)
      return (printf("Team %c won\n", team->id));
    cur = cur->next;
  }
  return (0);
}

static void	check_slot(t_team *team, unsigned int mclient)
{
  team->nb_player--;
  if (team->slot + team->nb_player < mclient)
    team->slot++;
}

t_team		*new_team(const char *nameteam)
{
  static int	id = 0;
  t_team	*team;

  if ((team = (struct s_team *)malloc(sizeof(struct s_team))) != NULL)
  {
    team->id = id;
    team->name = strdup(nameteam);
    team->nb_player = 0;
    team->nb_maxlvl = 0;
    team->check_slot = &check_slot;
    team->egg = new_list();
    id++;
  }
  return (team);
}
